package cn.laiguowei.May3.MD5Signature.controller;

import cn.laiguowei.May3.MD5Signature.bean.KeySecurity;
import cn.laiguowei.May3.MD5Signature.service.KeySecurityService;
import cn.laiguowei.May3.MD5Signature.service.inter.IKeySecurityService;
import cn.laiguowei.May3.MD5Signature.utils.UuidSecurityUitl;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.laiguowei.May3.MD5Signature.utils.AesSignatureAndMD5Utils;
import cn.laiguowei.May3.MD5Signature.utils.ValCommonRequest;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/aes")
public class AesSignationController {

	@Autowired
	KeySecurityService keySecurityService;
	
	
	@RequestMapping("getData")
	public String getData(ValCommonRequest valCommonRequest) {
		String key = "dDPOl95H5f7AKeJA4XgDFw==";  //秘钥
		String decoderStr = AesSignatureAndMD5Utils.ascDecode(valCommonRequest.getSecurityBody(),key);
		return decoderStr;
	}

	@RequestMapping("generateKeyAndSecurityKey")
	public  Map<String, String> generateKeyAndSecurityKey(String identityId){
		if(StringUtils.isBlank(identityId)){
			Map<String,String>map = new HashMap<>();
			map.put("error","请传入identityId");
			return map;
		}
		Map<String,String> map =  UuidSecurityUitl.generateKeyAndSecurity();
		KeySecurity keySecurity = new KeySecurity();
		keySecurity.setCode(map.get("key"));
		keySecurity.setSecurityKey(map.get("securityKey"));
		keySecurity.setIdentityId(identityId);
		keySecurity.setCreateTime(new Date());
		keySecurity.setModifyTime(new Date());
		keySecurityService.save(keySecurity);
		return map;
	}

	@RequestMapping("getSecrityByIndentityId/{identityId}")
	public KeySecurity getSecrityByIndentityId(@PathVariable  String identityId){
		return keySecurityService.getKeySecurityByidentityId( identityId);
	}
	
}
